//app.js
const icon_err = "../../image/err_w.png";
let config = require('/config.js');
// let util = require('util/util.js');

App({
  // 系统设置
  systemSetings: {
    showVibrate: true, //振动
    showSound: true, //声音
    showUserName: true //聊天界面是否显示用户名
  },
  // 通讯录数据
  contactsList: [],
  // 录音数据
  recordMsgArr: [
    {
      id: 1494638415591,
      stats: 1,
      userFlag: "/image/home_se.png",
      nickName: "bbbbb",
      filepath: "",
      recordLength: 50,
      times: 1000,
      showTime: 1
    }
  ],
  // socket 是否连接
  isopenSocket: false,
  // 登陆账号信息
  userInfo: {},
  // 是否已登录
  islogined: false,
  // 系统信息
  systemInfo: {},
  getSystemInfo:function(){
    let that=this;
    wx.getSystemInfo({
      success: function(res) {
        // console.log('getSystemInfo',res);
        that.systemInfo=res;
      },
    })
  },
  loginAccount: function() {
    let that = this
    wx.getStorage({
      key: 'bfdx_login_account',
      success: function (res) {
        if (res.data){
          that.account = JSON.parse(res.data)
        }
      }
    })
  },
  // 识别到用户尚未登录时调用
  jumpTo:function(){
    wx.showToast({
      title: '请先登陆',
      image: icon_err,
      mask: true
    });
    setTimeout(function () {
      wx.switchTab({
        url: "/page/index/index"
      });
    }, 1000);
  },
  showLoginingToast:function(){
    wx.showToast({
      title: '正在登陆',
      icon: "loading",
      duration: 3 * 60 * 1000,
      mask: true
    })
  },
  showLoginSucToast: function () {
    wx.showToast({
      title: '登陆成功',
      icon: "success"
      // mask: true
    })
  },
  showgetInfoToast: function () {
    wx.showToast({
      title: '正在获取账号信息',
      icon: "loading",
      duration: 3 * 60 * 1000,
      mask: true
    })
  },
  // socket连接
  connectSocket:function(){
    let that=this;
    wx.connectSocket({
      url: config.socketUrl,
      success:function(res){
        wx.onSocketOpen(function () {
          console.log(new Date(),'onSocketOpen')
          that.isopenSocket = true

          wx.onSocketMessage(function (res) {
            // console.log('收到服务器内容：', res.data);
            let rdata = res.data;
            // 解析收到的消息
            if (rdata.indexOf('server get message:') === -1) { return }
            rdata = rdata.replace('server get message:', '');
            rdata = JSON.parse(rdata);
            console.log(new Date(),'收到服务器内容：', rdata);
          })
        })

        wx.onSocketError(function () {
          console.log(new Date(),'onSocketError')
          that.isopenSocket = false;
        })
        wx.onSocketClose(function (res) {
          console.log(new Date(),'onSocketClose')
        })
      }
    })
  },
  // 登录后的操作
  afterLogined: function (data) {
    // console.log('afterLogined',data);
    // 同步app全局变量数据
    this.islogined = true;

    let selfData = data.self;
    this.userInfo = selfData;

    let setting = JSON.parse(selfData.setting);
    this.systemSetings = setting;

    this.contactsList = data.userRows;
  },
  onLaunch: function () {
    // when the cxapp init,it will be operate only once.
    this.loginAccount();
    // this.getSystemInfo();
    this.connectSocket();
  },
  onShow: function () {
    // when the cxapp start or form back end move to front end , it will be operate.
  },
  onHide: function () {
    // when the cxapp move to back end , it will be operate.
  },
  onError: function (msg) {
    // when the cxapp have a mistake or failed using wx's api, it will be operate.
    console.log(msg)
  }
})